package com.arm.service.impl;

import com.arm.dao.UserAccountLogMapper;
import com.arm.dao.UserAccountMapper;
import com.arm.module.UserAccount;
import com.arm.module.UserAccountLog;
import com.arm.service.BaseService;
import com.arm.service.UserAccountService;
import com.arm.service.UserService;
import com.arm.tools.myRunTime.MyRunTimeExcption;
import com.arm.tools.result.ReturnCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Baggio on 2019-6-5.
 */
@Service
public class UserAccountServiceImpl  extends BaseService implements UserAccountService {
    @Autowired
    UserAccountMapper userAccountMapper;
    @Autowired
    UserAccountLogMapper userAccountLogMapper;
    @Autowired
    UserService userService;
    @Override
    public void addUserAccount(UserAccount userAccount) throws MyRunTimeExcption {
        userAccountMapper.insertUserAccount(userAccount);
    }
    @Override
    public UserAccount getUserAccount(Map<String, String> map) throws MyRunTimeExcption {
        return userAccountMapper.getUserAccount(map);
    }

    @Override
    public void createUserAccount(Long userId, Byte userAccountType,Long storeId, Byte operatorId) throws MyRunTimeExcption {

    }

    @Override
    public void modifyUserAccount(Long userId, Byte userAccountType, Integer balance, String remark) throws MyRunTimeExcption {


        UserAccount userAccount = getUserAccount(new HashMap<String, String>() {
            {
                put("userId", String.valueOf(userId));
                put("userAccountType", String.valueOf(userAccountType));
            }
        });

        if (userAccount == null) {
            userAccount = new UserAccount();
            userAccount.setStatus(UserAccount.STATUS_VALID);
            userAccount.setAccountType(userAccountType);
            userAccount.setUserId(userId);
            userAccount.setBalance(0);
            userAccount.setCreateTime(new Date());
            addUserAccount(userAccount);
        }
        if(userAccount.getStatus()!=UserAccount.STATUS_VALID){
            throw new MyRunTimeExcption( ReturnCode.USER_ACCOUNT_UNVALID);
        }
        UserAccountLog userAccountLog = new UserAccountLog();
        userAccountLog.setCreateTime(new Date());
        userAccountLog.setUserId(userId);
        userAccountLog.setAccountType(userAccountType);
        userAccountLog.setBalance(balance);
        userAccountLog.setBeforeBalance(userAccount.getBalance());
        userAccountLog.setRemark(remark);
        Integer afterBalance = 0;
        afterBalance = userAccount.getBalance() + balance;
        logger.info(userAccount.getBalance() +"<==============setBeforeBalance=========>"+ balance);
        logger.info("============afterBalance======>>>>>>>"+afterBalance);
        if (afterBalance < 0) {
            throw new MyRunTimeExcption( ReturnCode.USER_ACCOUNT_BALANCE_INSUFFICIENT);
        }
        userAccountLog.setAfterBalance(afterBalance);
        userAccount.setBalance(afterBalance);
        userAccountMapper.updateUserAccountByPrimaryKeySelective(userAccount);
        userAccountLogMapper.insertUserAccountLog(userAccountLog);
    }

    @Override
    public List<UserAccount> getUserAccountListByUserId(Long userId) throws MyRunTimeExcption {


        return userAccountMapper.getUserAccountListByUserId(userId);
    }

    @Override
    public List<UserAccountLog> getUserAccountLogListByUserId(Long userId) throws MyRunTimeExcption {
        return userAccountLogMapper.getUserAccountLogListByUserId(userId);
    }

}
